package com.lc.luzhengtang.activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lc.luzhengtang.R;
import com.lc.luzhengtang.base.BaseActivity;
import com.lc.luzhengtang.bean.CommenBean;
import com.lc.luzhengtang.bean.ForgetBean;
import com.lc.luzhengtang.bean.VerificationCodeBean;
import com.lc.luzhengtang.conn.ConnChangePwd;
import com.lc.luzhengtang.conn.ConnRepassword;
import com.lc.luzhengtang.conn.ConnVerificationCode;
import com.lc.luzhengtang.util.LovelyToast;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilMatches;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/5/5.
 */
public class ForgetPasswordActivity extends BaseActivity {

    @BindView(R.id.img_news)
    ImageView imgNews;
    @BindView(R.id.imageView)
    TextView imageView;
    @BindView(R.id.edit_phone)
    EditText editPhone;
    @BindView(R.id.text_yanzhengcode)
    TextView textYanzhengcode;
    @BindView(R.id.edit_code)
    EditText editCode;
    @BindView(R.id.text_sendbtn)
    TextView textSendbtn;
    @BindView(R.id.text_newpwd)
    TextView textNewpwd;
    @BindView(R.id.edit_pwd_pay)
    EditText editPwdPay;
    @BindView(R.id.text_repasword)
    TextView textRepasword;
    @BindView(R.id.edit_pwd)
    EditText editPwd;
    @BindView(R.id.btn_settings)
    Button btnSettings;
    @BindView(R.id.text_title)
    TextView textTitle;
    @BindView(R.id.text_forget_password)
    TextView textForgetPassword;
    @BindView(R.id.layout_fourget)
    LinearLayout layoutFourget;
    private CountDownTimer countTime = new CountDownTimer(61000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {

            if (!isFinishing()) {

                textSendbtn.setText(millisUntilFinished / 1000 + "s重新获取");

            }
        }

        @Override
        public void onFinish() {

            if (!isFinishing()) {
                textSendbtn.setText("重新获取");
                textSendbtn.setEnabled(true);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_pwd);
        ButterKnife.bind(this);
        textTitle.setText("忘记支付密码");
        layoutFourget.setVisibility(View.GONE);
    }

    public void getChangePwd() {
        new ConnChangePwd(editPhone.getText().toString().trim(), editPhone.getText().toString().trim(), new AsyCallBack<ForgetBean>() {
            @Override
            public void onSuccess(String toast, int type, Object o, ForgetBean forgetBean) throws Exception {
                super.onSuccess(toast, type, o, forgetBean);
                LovelyToast.makeText(ForgetPasswordActivity.this, forgetBean.getMessage(), 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
                finish();
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
            /*    LovelyToast.makeText(AppealActivity.this, ConnChangePwd.TOAST, 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);*/
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
             /*   LovelyToast.makeText(AppealActivity.this, ConnChangePwd.TOAST, 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);*/
            }
        }

        ).execute(ForgetPasswordActivity.this, false);
    }

    @OnClick({R.id.img_news, R.id.imageView, R.id.edit_phone, R.id.text_yanzhengcode, R.id.edit_code, R.id.text_sendbtn, R.id.text_newpwd, R.id.edit_pwd_pay, R.id.text_repasword, R.id.edit_pwd, R.id.btn_settings, R.id.text_forget_password})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_news:
                finish();
                break;
            case R.id.imageView:
                break;
            case R.id.edit_phone:
                break;
            case R.id.text_yanzhengcode:
                break;
            case R.id.edit_code:
                break;
            case R.id.text_sendbtn:
                if (TextUtils.isEmpty(editPhone.getText().toString().trim())) {
                    LovelyToast.makeText(ForgetPasswordActivity.this, "用户手机号不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (editPhone.getText().toString().trim().length() < 11) {
                    LovelyToast.makeText(ForgetPasswordActivity.this, "手机号不能小于11位", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (!UtilMatches.checkMobile(editPhone.getText().toString())) {
                    LovelyToast.makeText(ForgetPasswordActivity.this, "请输入正确的手机号", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                    // UtilToast.show(RegisterActivity.this, "请输入正确的手机号");
                } else {
                    textSendbtn.setEnabled(false);
                    getVerificationCode();
                }

                break;
            case R.id.text_newpwd:
                break;
            case R.id.edit_pwd_pay:
                break;
            case R.id.text_repasword:
                break;
            case R.id.edit_pwd:
                break;
            case R.id.btn_settings:
                if (TextUtils.isEmpty(editPhone.getText().toString().trim())) {
                    LovelyToast.makeText(ForgetPasswordActivity.this, "用户手机号不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (editPhone.getText().toString().trim().length() < 11) {
                    LovelyToast.makeText(ForgetPasswordActivity.this, "手机号不能小于11位", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (!UtilMatches.checkMobile(editPhone.getText().toString())) {
                    LovelyToast.makeText(ForgetPasswordActivity.this, "请输入正确的手机号", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                    // UtilToast.show(RegisterActivity.this, "请输入正确的手机号");
                } else if (TextUtils.isEmpty(editCode.getText().toString().trim())) {
                    LovelyToast.makeText(ForgetPasswordActivity.this, "验证码不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (TextUtils.isEmpty(editPwdPay.getText().toString())) {
                    LovelyToast.makeText(ForgetPasswordActivity.this, "密码不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (TextUtils.isEmpty(editPwdPay.getText().toString())) {
                    LovelyToast.makeText(ForgetPasswordActivity.this, "密码不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (editPwdPay.getText().toString().length() < 6) {
                    LovelyToast.makeText(ForgetPasswordActivity.this, "请输入6~12位的密码", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (TextUtils.isEmpty(editPwd.getText().toString())) {
                    LovelyToast.makeText(ForgetPasswordActivity.this, "密码不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (!editPwd.getText().toString().equals(editPwdPay.getText().toString())) {
                    LovelyToast.makeText(ForgetPasswordActivity.this, "两次输入的密码不一致", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else {
                    settingspayPwd();
                }

                break;

            case R.id.text_forget_password:
                //忘记支付密码


                break;
        }
    }

    public void getVerificationCode() {
        new ConnVerificationCode(editPhone.getText().toString().trim(), "shi", new AsyCallBack<VerificationCodeBean>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
                showLoading();
            }

            @Override
            public void onSuccess(String toast, int type, Object o, VerificationCodeBean registerBean) throws Exception {

                LovelyToast.makeText(ForgetPasswordActivity.this, registerBean.getMessage(), 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
//                btRegistered.setSelected(true);
                countTime.start();
                super.onSuccess(toast, type, o, registerBean);
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                dismissLoading();
                textSendbtn.setEnabled(true);
                textSendbtn.setText("获取验证码");
                countTime.start();
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                dismissLoading();
                countTime.start();
            }
        }


        ).execute(this);
    }

    // edit_phone edit_pwd_pay
    public void settingspayPwd() {
        new ConnRepassword(editPhone.getText().toString(), editPwd.getText().toString(), new AsyCallBack<CommenBean>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
            }

            @Override
            public void onSuccess(String toast, int type, Object o, CommenBean commenBean) throws Exception {
                super.onSuccess(toast, type, o, commenBean);
                if (commenBean.getCode() == 200) {
                    LovelyToast.makeText(ForgetPasswordActivity.this, "设置密码成功", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                    finish();
                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
            }
        }


        ).execute(ForgetPasswordActivity.this, false);
    }


}
